function fetchNewItems(items,newItemsNumber){
	if(currentPage == Pages.HOME && items.length > 0){
		fetchItemsIntoTable(items.slice(0,newItemsNumber), 4);
	}
}


function refetchNewItemsFromCache(){
	fetchNewItems(newItemsArr,4);
	restoreHomelastItemPublishTime();
}

function fetchItemsIntoTable(items, maxRows){
	var even = false;
	var table = document.getElementById("itemtsablebody");
	for(var i = items.length - 1; i > -1; i--){
		var row = table.insertRow(0);
		if(even = !even)
			row.setAttribute('class', 'alt');
		var cell1 = row.insertCell(0);
		row.onclick = function() { viewItem(this.rowIndex); }; 
		var thumbnail = document.createElement('img');
		thumbnail.setAttribute('src', items[i].pictureURL);
		thumbnail.setAttribute('class', 'thumbnail');
		cell1.appendChild(thumbnail);
		var div = document.createElement('div');
		div.setAttribute('class', 'itemtext');
		div.innerHTML +=  "&nbsp;&nbsp;&nbsp;" + items[i].name + ":" + items[i].description ;
		cell1.appendChild(div);
		row.insertCell(1);
		row.insertCell(2);
		if(table.rows.length > maxRows){
			table.removeChild(table.lastChild);
		}
		//so that we will keep only 4 elements in the same table
	}
	//to update the current bid amount and time left
		updateItemsDetails();
}



//fetch searched items
function fetchSearchedItems(items){
	if(currentPage == Pages.SEARCH && items.length > 0){
		document.getElementById("itemtsablebody").innerHTML = "";
		fetchItemsIntoTable(items, 20);	
	}
}
	
//posting a new item 

function postItem(){
	var params = new Object();
	var innerdocument = getNestedPage().contentDocument;
	params.name = innerdocument.getElementById("productname").value;
	params.description = innerdocument.getElementById("productdescription").value;
	params.pictureURL = innerdocument.getElementById("pictureURL").value;
	params.currentBitAmount = innerdocument.getElementById("initialBitAmount").value;
	params.sellTimout = innerdocument.getElementById("selltimoutSeconds").value * 1000;
	params.sellTimout += innerdocument.getElementById("selltimoutMinutes").value * 1000 * 60;
	params.sellTimout += innerdocument.getElementById("selltimoutHours").value * 1000 * 60 * 60 * 24;
	params.sellTimout += innerdocument.getElementById("selltimoutDays").value * 1000 * 60 * 60 * 24;
	newItem(params);
}

function validRange(number,min,max,int){
	return (number > min && number < max && (!int || number % 1 == 0));
}

function checkValidTime(){
	var innerdocument = getNestedPage().contentDocument;
	var timeFields = ["selltimoutDays","selltimoutMinutes","selltimoutHours","selltimoutSeconds"];
	for(var i = 0; i < timeFields.length; i++){
	if(!validRange(innerdocument.getElementById(timeFields[i]).value,-1,60,true)){
			showError("time input must be 0-59");
	}	
	}
}

var inf = 1.7976931348623157E+10308;

function checkValidOpeningPrice(){
	var innerdocument = getNestedPage().contentDocument;
	if(!(validRange(innerdocument.getElementById("initialBitAmount").value,-1,inf,false))){
		showError("opening price must be positive");
	}
}



//const string 
var dollarString = "$", left = "left",minString = "m ",secString="sec ",SOLD_FOR = "sold for ";
var SOLD_TO = "sold to ";

function getTimeString(date){
	var timeLeft = date - new Date().getTime();
	var sec = timeLeft/1000;
	if(sec == 0){
		updateItemsDetailsArray();
	}
	return (true/*sec < 60*/) ? parseInt(sec) + secString + left: parseInt(sec/60) + minString + left;
}

//filing the view item page
function fillViewItemPage(){
	var innerdocument = getNestedPage().contentDocument;
	innerdocument.getElementById('itemname').innerHTML = selectedItem.name;
	innerdocument.getElementById('itemdecsription').innerHTML = selectedItem.description;
	innerdocument.getElementById("photo").setAttribute("src", selectedItem.pictureURL);
	innerdocument.getElementById('itemid').value = selectedItem.id;
	getNewComments(true);
	if(isLogged){
		showPostCommentForm();
		if(!selectedItem.mine && !isItemSold(selectedItem)){
			showPlaceBidform();
		}
	}
}


//update item new details
function updateItemsDetails(){
	if(currentPage == Pages.VIEWITEM){
		updateCurrentItem();
		}else{
			if(currentPage == Pages.SEARCH){
			updateItemsDetailsArray(searchedItems);
			}else if(currentPage == Pages.HOME){
			updateItemsDetailsArray(newItemsArr);
			}
}
}

function updateItemsDetailsArray(itemsArr){
	var tabl = document.getElementById("itemtsablebody");
	if(tabl.rows == undefined)
		return;
	var rowsLength = tabl.rows.length;
	for(var i=0;i<rowsLength;i++){
		if(isItemSold(itemsArr[i])){
			tabl.rows[i].cells[1].innerHTML = SOLD_FOR  + getStringWithDollarSign(itemsArr[i].currentBidAmount);
			tabl.rows[i].cells[2].innerHTML = SOLD_TO + itemsArr[i].winnerUserName;
		}else{
			tabl.rows[i].cells[1].innerHTML = getStringWithDollarSign(itemsArr[i].currentBidAmount);
			tabl.rows[i].cells[2].innerHTML = getTimeString(itemsArr[i].sellTime);
		}
	}
} 

function updateCurrentItem(){
	var innerdocument = getNestedPage().contentDocument;
	var currentBit = innerdocument.getElementById("currentbid");
	var timeLeft = innerdocument.getElementById("timeleft");
	if(isItemSold(selectedItem)){
		currentBit.innerHTML = SOLD_FOR  + getStringWithDollarSign(selectedItem.currentBidAmount);
		timeLeft.innerHTML = SOLD_TO + selectedItem.winnerUserName;
		hidePlaceBidform();
		}else{
			currentBit.innerHTML = getStringWithDollarSign(selectedItem.currentBidAmount);
			timeLeft.innerHTML = getTimeString(selectedItem.sellTime);
		}
	}


function getStringWithDollarSign(string){
	return string + dollarString;
}


function viewItem(index){
	var mypage = currentPage;
	switchToViewItem();
	
	if(mypage == Pages.SEARCH){
		selectedItem = searchedItems[index];
	}else if(mypage == Pages.HOME){
		selectedItem = newItemsArr[index];
	}
	setTimeout(function() {
		fillViewItemPage();
		updateCurrentItem(); 
	},80);
}

function search(){
	switchToSearch();
	emptyCachedItems();
	var searchText = document.getElementById("searchtext").value;
	searchForItems(searchText);
}


